//
//  ChatImageCell.m
//  BGM
//
//  Created by 我要变好看 on 2019/1/15.
//  Copyright © 2019 com.alirtc.demo. All rights reserved.
//

#import "ChatImageCell.h"
#import "Common.h"
#import "BGMUser.h"
#import "UIImageView+WebCache.h"


@interface ChatImageCell ()


@property (nonatomic,strong)UIImageView *headImage;
@property (nonatomic,strong)UIImageView *bgTxtImage;

@property (nonatomic,strong)NSString *userId;
@property (nonatomic,strong)NIMMessage *message;


@end


@implementation ChatImageCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self != nil) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        self.headImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
        self.headImage.layer.cornerRadius = 20;
        self.headImage.layer.masksToBounds = YES;
        self.headImage.userInteractionEnabled = YES;
        [self addSubview:self.headImage];
//        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickHead)];
//        [self.headImage addGestureRecognizer:tap];
        
        
        self.bgTxtImage = [[UIImageView alloc]init];
        self.bgTxtImage.backgroundColor = [UIColor redColor];
        [self addSubview:self.bgTxtImage];
        
       

        
    }
    return self;
}

- (void)chatImage:(NIMMessage *)message{
    
    self.message = message;
    
    NSString *meUerId = [BGMUser shareInstance].userId;
    NSString *userImage = [BGMUser shareInstance].headImage;
    NSString *formId = message.from;
    NIMUser *user = [[[NIMSDK sharedSDK]userManager]userInfo:formId];
    self.userId = user.userId;
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:user.userInfo.avatarUrl] placeholderImage:[UIImage imageNamed:@"headImage"]];
   
    NIMImageObject *object = message.messageObject;
 
    if (![meUerId isEqualToString:formId]) { //左
        self.headImage.frame = CGRectMake(10, 10, 40, 40);
        self.bgTxtImage.frame = CGRectMake(55, 20, 100, 100);
        
    }else{
        self.headImage.frame = CGRectMake(SCREENW-70, 10, 40, 40);
        [self.headImage sd_setImageWithURL:[NSURL URLWithString:userImage] placeholderImage:[UIImage imageNamed:@"headImage"]];
        self.bgTxtImage.frame = CGRectMake(SCREENW-100-60-25, 20, 100, 100);
    }
    
    
    [self.bgTxtImage sd_setImageWithURL:[NSURL URLWithString:object.url] placeholderImage:[UIImage imageNamed:@"imageNormal"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        float imageW = 0.0;
        float imageH = 0.0;
        
        if (image.size.height>image.size.width) {  //u竖
            if(image.size.height>200){
                imageH = 200;
                imageW = (image.size.width*imageH)/image.size.height;
            }else{
                imageH = image.size.height;
                imageW = image.size.width;
            }
        }else{  //横
            
            if(image.size.width>200){
                imageW = 200;
                imageH = (image.size.height*imageW)/image.size.width;
            }else{
                
                imageH = image.size.height;
                imageW = image.size.width;
            }
            
        }
        
        
        
        if (![meUerId isEqualToString:formId]) { //左
            self.bgTxtImage.frame = CGRectMake(55, 20, imageW, imageH);
        }else{
            self.bgTxtImage.frame = CGRectMake(SCREENW-imageW-60-25, 20, imageW, imageH);
        }
    }];
  
}

+ (void)chatImageHight:(NIMMessage *)message finished:(void(^)(CGFloat width,CGFloat height))finished
{
    NIMImageObject *object = message.messageObject;
    UIImageView *imageView = [[UIImageView alloc]init];
    
    [imageView sd_setImageWithURL:[NSURL URLWithString:object.url] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        float imageW = 0.0;
        float imageH = 0.0;
        
        if (image.size.height>image.size.width) {  //u竖
            if(image.size.height>200){
                imageH = 200;
                imageW = (image.size.width*imageH)/image.size.height;
            }else{
                imageH = image.size.height;
                imageW = image.size.width;
            }
        }else{  //横
            if(image.size.width>200){
                imageW = 260;
                imageH = (image.size.height*imageW)/image.size.width;
            }else{
                imageH = image.size.height;
                imageW = image.size.width;
            }
        }
        
        if(finished){
            finished(imageW,imageH);
        }
        
    }];
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
